package com.javatiaocao.myblog.controller;

import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.service.RedisService;
import com.javatiaocao.myblog.service.StatisticsInfoService;
import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class StatisticsController {
    @Autowired
    private StatisticsInfoService statisticsInfoService;

    /**
     *
     * 访客量
     *
     * **/
    @GetMapping("/getVisitorNumByPageName")
    public String getVisitorNumByPageName(HttpServletRequest request, @RequestParam("pageName") String pageName){
        try {
            int index = pageName.indexOf("/");
            if (index == -1){
                pageName = "visitorVolume";
            }
            DataMap dataMap = statisticsInfoService.getVisitorNumByPageName(pageName,request);

            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("getVisitorNumByPageName is error" + e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     *
     *
     * 获取仪表盘信息
     *
     * **/
    @GetMapping("/getStatisticsInfo")
    public String getStatisticsInfo(HttpServletRequest request, @AuthenticationPrincipal UserDetails userDetails){
        try {

            DataMap dataMap = statisticsInfoService.getStatisticsInfo(request,userDetails);
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("getStatisticsInfo is error"+e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();

    }
}
